from fastmcp.server import FastMCP
from fastmcp.tools import Tool
import requests

# 让AI生成Playwright代码的函数
def ai_make_script(request):
    user_want = request.params.get("需求", "")
    # 调用通义千问API（简化版，具体签名看官方文档）
    api_url = "https://qianwen-api.aliyun.com/v1/chat/completions"
    headers = {"Content-Type": "application/json", "Authorization": "你的密钥"}
    data = {
        "model": "qwen-turbo",
        "messages": [{"role": "user", "content": f"生成Playwright Python代码，做这些：{user_want}，只给代码别解释"}]
    }
    res = requests.post(api_url, json=data).json()
    return res["choices"][0]["message"]["content"]

# 启动MCP服务
server = FastMCP()
# 注册"生成自动化脚本"的工具
aio_tool = Tool.from_function(
    ai_make_script,
    name="playwright_helper",
    description="根据自然语言需求生成Playwright代码"
)
server.add_tool(aio_tool)

server.run()  # 启动后别关这个终端！